package com.cxq.finalproject;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.cxq.finalproject.databinding.ActivityUpdateUserInfoBinding;
import com.cxq.finalproject.util.MyUserHelper;

public class Update_userInfo extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    //声明bing
    private ActivityUpdateUserInfoBinding binding;
    SharedPreferences sp;
    //    数据库操作类
    MyUserHelper myUserHelper;
    private String sex = "男";
    SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_user_info);
        // 打开数据表库表
        myUserHelper = new MyUserHelper(this, MyUserHelper.DB_NAME, null, 1);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_update_user_info);
        binding.radioGroup1.setOnCheckedChangeListener(this);
        sp = getSharedPreferences("Login_UserInfo", MODE_PRIVATE);
        editor = sp.edit();
        //初始化控件
        binding.setUpdateUserName(sp.getString("userName",""));
        if (sp.getString("isRem", "").equals("1")){
            binding.radioButton.setChecked(true);
        }else{
            binding.radioButton2.setChecked(true);
        }
        binding.setUserId(sp.getString("userId",""));

        binding.button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(binding.editTextTextPersonName4.getText().toString().equals("") ){
                    Toast.makeText(getApplicationContext(), "姓名不能为空!",
                            Toast.LENGTH_SHORT).show();
                }else if(binding.editTextTextPersonName5.getText().toString().equals("")){
                    Toast.makeText(getApplicationContext(), "用户名不能为空!",
                            Toast.LENGTH_SHORT).show();
                }else if(binding.editTextTextPassword4.getText().toString().equals("")){
                    Toast.makeText(getApplicationContext(), "原密码不能为空!",
                            Toast.LENGTH_SHORT).show();
                }else if(binding.editTextTextPassword5.getText().toString().equals("")){
                    Toast.makeText(getApplicationContext(), "新密码不能为空!",
                            Toast.LENGTH_SHORT).show();
                }else if(!binding.editTextTextPassword4.getText().toString().equals(sp.getString("password",""))){
                    Toast.makeText(getApplicationContext(), "原密码不正确!",
                            Toast.LENGTH_SHORT).show();
                }else if(isExist() && !binding.editTextTextPersonName5.getText().toString().equals(sp.getString("userId",""))){
                    Toast.makeText(getApplicationContext(), "用户名已存在!",
                            Toast.LENGTH_SHORT).show();
                }
                else{
                    updateUser();
                    editor.putString("userName",binding.editTextTextPersonName4.getText().toString());
                    editor.putString("password",binding.editTextTextPassword5.getText().toString());
                    editor.putString("userId",binding.editTextTextPersonName5.getText().toString());
                    editor.commit();
                    Toast.makeText(getApplicationContext(), "更新成功",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        RadioButton radio = (RadioButton) findViewById(checkedId);
        sex = radio.getText().toString();
    }

    public void updateUser(){
        // 获得数据库对象
        SQLiteDatabase db = myUserHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(MyUserHelper.ID,  binding.editTextTextPersonName5.getText().toString());
        values.put(MyUserHelper.PWD, binding.editTextTextPassword5.getText().toString());
        values.put(MyUserHelper.UserNAME, binding.editTextTextPersonName4.getText().toString() );
        values.put(MyUserHelper.GANDER, sex );

        // 更新数据，在原来的userId上
        long rid = db.update(MyUserHelper.TABLE_NAME,values,"userId=?",
                new String[]{sp.getString("userId","")});
        db.close();
    }

    public Boolean isExist(){
        boolean rid = false; // 插入数据
        // 获得数据库对象
        SQLiteDatabase db = myUserHelper.getReadableDatabase();
        Cursor c = db.query(MyUserHelper.TABLE_NAME, new String[] {
                        MyUserHelper.ID, MyUserHelper.PWD, MyUserHelper.UserNAME,},
                MyUserHelper.ID + "=?",
                new String[] { binding.editTextTextPersonName5.getText().toString()}, null, null, null);
        if (c.getCount() > 0) {
            rid = true;
        }
        db.close();
        return rid;
    }
}